package com.dyw.Day09;

/**
 * Devil
 *
 * @author Devil
 * @create 2022-01-18-21:47
 */
public class Demo01 {
    public int myAtoi(String s) {
        //1. 去掉字符串前导空格
        s = s.trim();
        if(s.length()==0){
            return 0;
        }
        //2. 判断符号
        int flag = 1;
        int index = 0;
        int res = 0;
        if(s.charAt(index)=='-'|| s.charAt(index)=='+'){
            flag = s.charAt(index++) == '+'? 1:-1;

        }

        for(; index<s.length();index++){
            int digit = s.charAt(index)-'0';
            if(digit <0 || digit > 9){
                break;
            }
            if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
                return flag == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            } else {
                res = res * 10 + digit;
            }

        }
        return flag*res;
    }
}
